<?php
namespace Admin\Model;
use Think\Model;
class CompanyModel extends Model 
{
	protected $insertFields = array('company_name','admin_id','datum','company_principal','addr','tel','status','start_time','end_time');
	protected $updateFields = array('id','company_name','admin_id','datum','company_principal','addr','tel','status','start_time','end_time');
	protected $_validate = array(
		array('company_name', '1,255', '公司名的值最长不能超过 255 个字符！', 2, 'length', 3),
		array('company_principal', 'require', '法人代表不能为空！', 1, 'regex', 3),
		array('company_principal', '1,255', '法人代表的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('addr', 'require', '地址不能为空！', 1, 'regex', 3),
		array('addr', '1,255', '地址的值最长不能超过 255 个字符！', 1, 'length', 3),
		array('tel', 'require', '联系电话不能为空！', 1, 'regex', 3),
		array('tel', 'number', '联系电话必须是一个整数！', 1, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($company_name = I('get.company_name'))
			$where['company_name'] = array('like', "%$company_name%");
		if($admin_id = I('get.admin_id'))
			$where['admin_id'] = array('eq', $admin_id);
		if($datum = I('get.datum'))
			$where['datum'] = array('eq', $datum);
		if($company_principal = I('get.company_principal'))
			$where['company_principal'] = array('like', "%$company_principal%");
		if($addr = I('get.addr'))
			$where['addr'] = array('like', "%$addr%");
		if($tel = I('get.tel'))
			$where['tel'] = array('eq', $tel);
		if($status = I('get.status'))
			$where['status'] = array('like', "%$status%");
		if($start_time = I('get.start_time'))
			$where['start_time'] = array('eq', $start_time);
		if($end_time = I('get.end_time'))
			$where['end_time'] = array('eq', $end_time);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['pic']) && $_FILES['pic']['error'] == 0)
		{
			$ret = uploadOne('pic', 'Company', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['pic'] = $ret['images'][0];
				$data['big_pic'] = $ret['images'][1];
				$data['mid_pic'] = $ret['images'][2];
				$data['sm_pic'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['pic']) && $_FILES['pic']['error'] == 0)
		{
			$ret = uploadOne('pic', 'Company', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['pic'] = $ret['images'][0];
				$data['big_pic'] = $ret['images'][1];
				$data['mid_pic'] = $ret['images'][2];
				$data['sm_pic'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_pic'),
				I('post.old_big_pic'),
				I('post.old_mid_pic'),
				I('post.old_sm_pic'),
	
			));
		}
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('pic,big_pic,mid_pic,sm_pic')->find($option['where']['id']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}